<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="visible"
    width="700px"
    append-to-body
  >
    <el-form ref="form" :model="form" label-width="auto" :rules="rules">
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="appKey" prop="kvalue">
            <el-input
              v-model="form.kvalue"
              placeholder="appKey"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="标签列表" prop="labelList">
            <div class="label-list">
              <el-tag
                v-for="item in form.labelList"
                :key="item"
                closable
                @close="handleClose(tag)"
              >
                {{ item }}
              </el-tag>
              <el-input
                v-if="inputVisible"
                v-model="inputValue"
                ref="saveTagInput"
                size="small"
                class="input-new-tag"
                @keyup.enter.native="handleInputConfirm"
                @blur="handleInputConfirm"
              ></el-input>
              <el-button v-else class="button-new-tag" size="small" @click="showInput">+</el-button>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="热门" prop="enabled">
            <el-checkbox v-model="form.isHot"></el-checkbox>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="24">
          <el-form-item label="常见问题" prop="problemList">
            <div class="problem-list">
              <el-tag
                v-for="item in form.problemList"
                :key="item"
                closable
                @close="handleProblemClose(tag)"
              >
                {{ item }}
              </el-tag>
              <el-input
                v-if="problemInputVisible"
                v-model="problemInputValue"
                ref="saveProblemTagInput"
                size="small"
                class="input-new-tag"
                @keyup.enter.native="handleProblemInputConfirm"
                @blur="handleProblemInputConfirm"
              ></el-input>
              <el-button v-else class="button-new-tag" size="small" @click="showProblemInput">+</el-button>
            </div>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <!-- <el-col :span="12">
          <el-form-item label="启用" prop="enabled">
            <el-switch v-model="form.enabled"></el-switch>
          </el-form-item>
        </el-col> -->
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as types from "./types";
import { addGuide, modifyGuide } from '@/api/guide'
import { findTypeByValue } from "@/utils/findType"

export default {
  name: "",
  data() {
    return {
      rules: {
        name: [
          { required: true, message: "名称", trigger: "blur" },
        ],
        kvalue: [
          { required: true, message: "appKey", trigger: "blur" },
        ],
      },
      title: "",
      type: "",
      inputVisible: false,
      inputValue: "",
      problemInputVisible: false,
      problemInputValue: "",
      config: {
        edit: {
          func: modifyGuide,
          title: "编辑"
        },
        new: {
          func: addGuide,
          title: "新增"
        }
      },
      form: {
        name: "",
        labelList: [],
        problemList: [],
        kvalue: "",
        enabled: true
      },
      visible: false,
      types
    };
  },
  methods: {
    handleClose(tag) {
      this.form.labelList.splice(this.form.labelList.indexOf(tag), 1);
    },

    handleProblemClose(tag){
      this.form.problemList.splice(this.form.problemList.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput?.$refs?.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.form.labelList.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },

    showProblemInput() {
      this.problemInputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveProblemTagInput?.$refs?.input.focus();
      });
    },

    handleProblemInputConfirm() {
      let inputValue = this.problemInputValue;
      if (inputValue) {
        this.form.problemList.push(inputValue);
      }
      this.problemInputVisible = false;
      this.problemInputValue = '';
    },

    showDialog(row) {
      this.reset();
      const isEdit = !!row;
      this.visible = true;
      this.type = isEdit ? "edit" : "new";

      if (isEdit) {
        this.form = Object.assign(this.form, {
          ...row,
          enabled: row.enabled == "1",
          isHot: row.isHot == "1",
        });
      }
      this.title = this.config[this.type].title;
    },

    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.visible = false;
          const data = JSON.parse(JSON.stringify(this.form));
          data.labels = JSON.stringify(data.labelList);
          data.routineProblem = JSON.stringify(data.problemList);
          data.enabled = data.enabled ? "1" : "0";
          data.isHot = data.isHot ? "1" : "0";
          data.type = "0";
          if(this.type === 'new'){
            data.id = null;
          }
          delete data.labelList;
          delete data.problemList
          this.config[this.type]?.func(data).then(() => {
            this.$modal.msgSuccess("提交成功");
            this.visible = false;
            this.$emit("refresh");
          });
        } else {
          return false;
        }
      });
    },

    cancel() {
      this.visible = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.resetForm("form");
      this.$forceUpdate();
    },
  },
};
</script>
<style scoped lang="scss">
.label-list, .problem-list {
  min-height: 36px;
  display: flex;
  align-items: center;

  .el-tag{
    margin-right: 10px;
  }

  .button-new-tag{
    height: 28px;
    padding: 0 20px;
  }

  .input-new-tag{
    width: 100px;
    height: 28px;
    display: flex;

    ::v-deep .el-input__inner {
      width: 100%;
      height: 100%;
    }
  }
}

.problem-list {
  display: block
}
</style>
